Getting around in the REPL

To start the interactive shell (aka, REPL = Read Eval Print Loop), open a terminal and run

julia

The Julia REPL is modal. If you type a ? at the beginning of a line, you will enter help mode.


In [1]:
?cat


search: cat catch catalan catch_backtrace vcat hcat hvcat cartesianmap

Out[1]:
cat(dims, A...)

Concatenate the input arrays along the specified dimensions in the iterable dims. For dimensions not in dims, all input arrays should have the same size, which will also be the size of the output array along that dimension. For dimensions in dims, the size of the output array is the sum of the sizes of the input arrays along that dimension. If dims is a single number, the different arrays are tightly stacked along that dimension. If dims is an iterable containing several dimensions, this allows one to construct block diagonal matrices and their higher-dimensional analogues by simultaneously increasing several dimensions for every new input array and putting zero blocks elsewhere. For example, cat([1,2], matrices...) builds a block diagonal matrix, i.e. a block matrix with matrices[1], matrices[2], ... as diagonal blocks and matching zero blocks away from the diagonal.

Typing ; will enter a shell mode (not a full shell):


In [2]:
;pwd


/Users/jmxp/code/julia-for-pythonistas

In addition, the REPL has many of the usual line editing, tab completion, and history features we've come to expect.

Julia basics

Many things in Julia work like you'd expect:


In [3]:
1 + 2


Out[3]:
3

In [4]:
1. + 2


Out[4]:
3.0

In [5]:
1/2  # floating point division


Out[5]:
0.5

In [6]:
2^2  # ^, not **


Out[6]:
4

In [7]:
1//2  # Rational type, not integer division


Out[7]:
1//2

In [8]:
1 + 3 * im


Out[8]:
1 + 3im

In [9]:
conj(1 + 3*im)


Out[9]:
1 - 3im

In [10]:
(1 + 3im) * (2 + 4im)


Out[10]:
-10 + 10im

In [11]:
"This is a string!"  # double quotes only


Out[11]:
"This is a string!"

In [12]:
't'  # char


Out[12]:
't'

Arrays and slicing

Julia takes much of its notation for arrays from Matlab:


In [13]:
v = [1, 2, 3, 4]  # array literal syntax


Out[13]:
4-element Array{Int64,1}:
 1
 2
 3
 4

In [14]:
vv = [1 2 3 4]  # horizontal concatenation (hcat)


Out[14]:
1x4 Array{Int64,2}:
 1  2  3  4

In [15]:
vvv = [1 ; 2 ; 3 ; 4]  # vertical concatenation (vcat)


Out[15]:
4-element Array{Int64,1}:
 1
 2
 3
 4

In [16]:
v[1]  # indices start at 1, no negative indices (at least not for indexing from the end)


Out[16]:
1

In [17]:
v[2:end]  # end keyword --> length(v)


Out[17]:
3-element Array{Int64,1}:
 2
 3
 4

Slicing in Julia returns copy, not view (use sub or (v0.5) view)

Compat.jl == from __future__ import


In [18]:
A = [[1 2 3] ; [4 5 6]]


Out[18]:
2x3 Array{Int64,2}:
 1  2  3
 4  5  6

In [19]:
A[:]  # storage is column-major


Out[19]:
6-element Array{Int64,1}:
 1
 4
 2
 5
 3
 6

In [20]:
A'  # transpose


Out[20]:
3x2 Array{Int64,2}:
 1  4
 2  5
 3  6

Broadcasting and elementwise


In [21]:
A + 1


Out[21]:
2x3 Array{Int64,2}:
 2  3  4
 5  6  7

In [22]:
A * 2


Out[22]:
2x3 Array{Int64,2}:
 2   4   6
 8  10  12

In [23]:
A * [0, 0, 1]  # matrix multiplication


Out[23]:
2-element Array{Int64,1}:
 3
 6

In [24]:
A .* [0, 1]  # elementwise


Out[24]:
2x3 Array{Int64,2}:
 0  0  0
 4  5  6

In Julia, .<op> is elementwise <op>. In v0.5, this becomes syntactic sugar for map(<op>, ...).


In [25]:
A \ [5, 6]  # backsolve: Ax = b ==> x = A \ b


Out[25]:
3-element Array{Float64,1}:
 -2.05556 
  0.111111
  2.27778 

In [26]:
A ./ [5, 6]


Out[26]:
2x3 Array{Float64,2}:
 0.2       0.4       0.6
 0.666667  0.833333  1.0

In [27]:
A ./ [3 4 5]


Out[27]:
2x3 Array{Float64,2}:
 0.333333  0.5   0.6
 1.33333   1.25  1.2

In [28]:
A / diagm([1, 2, 3])  # diagm = make diagonal matrix; A / B = A * B^{-1}


Out[28]:
2x3 Array{Float64,2}:
 1.0  1.0  1.0
 4.0  2.5  2.0

Arrays are not quite lists


In [29]:
typeof(A)


Out[29]:
Array{Int64,2}

In [30]:
size(A)


Out[30]:
(2,3)

In [31]:
eltype(A)


Out[31]:
Int64
A[end] = 1.5 # error!

If we put disparate types in a list, Julia will try to figure out what common type is "big enough" for all of them:


In [32]:
[1.5, 1, -2, 3+2im]


Out[32]:
4-element Array{Complex{Float64},1}:
  1.5+0.0im
  1.0+0.0im
 -2.0+0.0im
  3.0+2.0im

If we want to be able to put anything in a list, the element type of the list should be Any:


In [33]:
aa = [1, 2.5, 'f', "foo"]


Out[33]:
4-element Array{Any,1}:
 1     
 2.5   
  'f'  
  "foo"

We can specify the type of a literal:


In [34]:
aa = Any[1, 2, 3]


Out[34]:
3-element Array{Any,1}:
 1
 2
 3

In [35]:
push!(aa, "foo")  # ! means "mutating"


Out[35]:
4-element Array{Any,1}:
 1     
 2     
 3     
  "foo"

In [36]:
[1, 2] + [3, 4]  # not concatenation


Out[36]:
2-element Array{Int64,1}:
 4
 6

Things we love from Python

Julia has the basic data types we've come to know and love:

Dictionaries


In [37]:
dd = Dict('a' => 1, 'b' => 2, 'c' => 3)  # no curly brace literals


Out[37]:
Dict{Char,Int64} with 3 entries:
  'b' => 2
  'c' => 3
  'a' => 1

In [38]:
keys(dd)


Out[38]:
Base.KeyIterator for a Dict{Char,Int64} with 3 entries. Keys:
  'b'
  'c'
  'a'

In [39]:
values(dd)


Out[39]:
Base.ValueIterator for a Dict{Char,Int64} with 3 entries. Values:
  2
  3
  1

In [40]:
merge!(dd, Dict('z' => 26, 'y' => 25))


Out[40]:
Dict{Char,Int64} with 5 entries:
  'y' => 25
  'b' => 2
  'z' => 26
  'c' => 3
  'a' => 1

In [41]:
dd


Out[41]:
Dict{Char,Int64} with 5 entries:
  'y' => 25
  'b' => 2
  'z' => 26
  'c' => 3
  'a' => 1

Sets


In [42]:
ss = Set([1, 2, 3, 4])


Out[42]:
Set([4,2,3,1])

In [43]:
3 in ss, 5 in ss, 3  ss


Out[43]:
(true,false,true)

In [44]:
typeof(ss)  # note: typed


Out[44]:
Set{Int64}

Tuples


In [45]:
typeof((1.1, 2))


Out[45]:
Tuple{Float64,Int64}

In [46]:
typeof(('a', 2))


Out[46]:
Tuple{Char,Int64}
(1, 2) + (3, 4) # still won't work

In [47]:
aa, bb = (1, 2), (3, 4)


Out[47]:
((1,2),(3,4))

In [48]:
(aa..., bb...)  # splat


Out[48]:
(1,2,3,4)
aa, bb... = (1, 2, 3, 4) # doesn't work (yet)

Comprehensions, etc.


In [49]:
[x^2 for x in 1:5]


Out[49]:
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

In [50]:
Dict(zip("abcde", 1:5))


Out[50]:
Dict{Char,Int64} with 5 entries:
  'd' => 4
  'b' => 2
  'e' => 5
  'c' => 3
  'a' => 1

In [51]:
[(a, b) for a=1:5, b=6:10]


Out[51]:
5x5 Array{Tuple{Int64,Int64},2}:
 (1,6)  (1,7)  (1,8)  (1,9)  (1,10)
 (2,6)  (2,7)  (2,8)  (2,9)  (2,10)
 (3,6)  (3,7)  (3,8)  (3,9)  (3,10)
 (4,6)  (4,7)  (4,8)  (4,9)  (4,10)
 (5,6)  (5,7)  (5,8)  (5,9)  (5,10)

In [52]:
for (k, v) in dd
    println("key $k, value: $v")
end


key y, value: 25
key b, value: 2
key z, value: 26
key c, value: 3
key a, value: 1

In [ ]: